package cn.fl.pbl.service.impl;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.*;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.pbl.service.IPblBillMService;
import cn.fl.pbl.transform.PblBillMVOTransform;
import cn.fl.pbl.vo.PblBillMVO;
import fl.pbl.facade.pbl.api.IPblBillMApiService;
import fl.pbl.facade.pbl.condition.PblBillMQC;
import fl.pbl.facade.pbl.constant.PblBillMConstant;
import fl.pbl.facade.pbl.dto.PblBillMDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;


/**
 * @Description: 垫款贷账单信息 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class PblBillMServiceImpl implements IPblBillMService {
	
	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(PblBillMServiceImpl.class);
	/**
	 * 垫款贷表暴露服务
	 */
	@Autowired
	private IPblBillMApiService iPblBillMApiService;
	/**
     * 数据字典服务
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;

	@Override
	public PblBillMVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			return null;
		}
		PblBillMDTO dto = this.iPblBillMApiService.selectPblBillMById(id);
		PblBillMVO vo = PblBillMVOTransform.toVO(dto);
		this.setDictData(vo);
		return vo;
	}

	@Override
	public Long add(PblBillMVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.newLogger("新增对象不存在!").setKeys("add",CurrentThreadContext.getCurrentUserName()).info();
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"新增对象不存在!");
		}
		//return this.pblBillMApiService.add(PblBillMVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.newLogger("删除对象主键不能为空!").setKeys("removeById",CurrentThreadContext.getCurrentUserName()).info();
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"删除对象主键不能为空!");
		}
		//return this.pblBillMApiService.removePblBillMDTOById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
		if(CollectionUtils.isEmpty(ids)){
			BaseLogUtils.newLogger("删除主键参数集合不能为空!").setKeys("removeByIds",CurrentThreadContext.getCurrentUserName()).info();
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"删除对象主键不能为空!");
		}
		return false;
    }

	@Override
	public boolean modify(PblBillMVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.newLogger("修改对象不能为空!").setKeys("modify",CurrentThreadContext.getCurrentUserName()).info();
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"修改对象不能为空!");
		}
		//return this.pblBillMApiService.modifyPblBillMDTO(PblBillMVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pager searchListPage(Pager page) throws BaseException{
//		List<PblBillDTO> list=new ArrayList<PblBillDTO>();
//		for(int i=0;i<5;i++){
//			PblBillDTO dto=new PblBillDTO();
//			Long aaa=Long.valueOf((i+1)*10000);
//			dto.setDzId(aaa);
//			dto.setPayId(170101104000006L);
//			dto.setPaySchNo("CNKYHP00048320174-1");
//			dto.setRate(0.1);
//			dto.setCrTyp("SJSH");
//			dto.setCstId(161231030000006L);
//			dto.setCstNm("磁县宏阳无害化处理有限公司");
//			dto.setFundRasInsId(aaa);
//			dto.setContNo("CCC"+aaa);
//			dto.setContAmt(7000000L+i);
//			dto.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
//			dto.setSysCd(CurrentThreadContext.getCurrentSysCd());	
//			list.add(dto);
//		}
//		List<DataResultDTO> list2 = this.iPblBillMApiService.addPblBillMForDzIds(list);
//		PblBillDQC condition1=new PblBillDQC();
//		List<Long> idList= new ArrayList<Long>();
//		idList.add(180703386000082L);
//		idList.add(180702386000081L);
//		idList.add(180629386000061L);
//		condition1.setPblIdList(idList);
//		List<PblBillDDTO> list = this.iPblBillMApiService.searchPblBillDList(condition1);
		PblBillMQC condition=null;
		if(null == page){
			page = new Pager();
			page.setCondition(new PblBillMQC());
		}
		condition=(PblBillMQC)page.getCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
		page = this.iPblBillMApiService.searchPblBillMPageList(page);
		if(CollectionUtils.isNotEmpty(page.getResultList())){
			List<PblBillMVO> voList = PblBillMVOTransform.toVOList((List<PblBillMDTO>)page.getResultList());
			this.setDictData(voList);
			page.setResultList(voList);
		}	
		return page;
	}

	@Override
	public List<PblBillMVO> searchList(PblBillMQC condition) throws BaseException {
		if(condition==null)condition = new PblBillMQC();
		List<PblBillMDTO> dtoList = this.iPblBillMApiService.searchPblBillMList(condition);
		return PblBillMVOTransform.toVOList(dtoList);
	}

	@Override
	public List<KeyValueVO> searchCdList(String groupCd) {
		return this.sysDictionaryApiService.getKvList(groupCd, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
	}
	
	/**
     * 添加数据字典名称
     * @param voList
     */
	private void setDictData(List<PblBillMVO> voList){
        if(CollectionUtils.isEmpty(voList)) return;
        Map<String, String> crTypCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), PblBillMConstant.PBL_CR_TYP_CD);
        Map<String, String> loanStsCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), PblBillMConstant.PBL_LOAN_STS_CD);
        Map<String, String> loanBizTypCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), PblBillMConstant.PBL_LOAN_BIZ_TYP_CD);
        Map<String, String> lsPdUntCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), PblBillMConstant.PBL_LS_PD_UNT_CD);
        Map<String, String> payWayCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(),PblBillMConstant.PBL_PAY_WAY_CD);
        //格式化保留两位小数
        DecimalFormat df = new DecimalFormat("######0.00");

        for(PblBillMVO vo:voList){
            if (crTypCdMap != null && crTypCdMap.containsKey(vo.getCrTypCd())) {
                vo.setCrTypCdNm(crTypCdMap.get(vo.getCrTypCd()));
            }
            if (loanStsCdMap != null && loanStsCdMap.containsKey(vo.getLoanStsCd())) {
                vo.setLoanStsCdNm(loanStsCdMap.get(vo.getLoanStsCd()));
            }
            if (loanBizTypCdMap != null && loanBizTypCdMap.containsKey(vo.getLoanBizTypCd())) {
                vo.setLoanBizTypCdNm(loanBizTypCdMap.get(vo.getLoanBizTypCd()));
            }
            if (lsPdUntCdMap != null && lsPdUntCdMap.containsKey(vo.getLsPdUntCd())) {
                vo.setLsPdUntCdNm(lsPdUntCdMap.get(vo.getLsPdUntCd()));
            }
            if (payWayCdMap != null && payWayCdMap.containsKey(vo.getPayWayCd())) {
                vo.setPayWayCdNm(payWayCdMap.get(vo.getPayWayCd()));
            }

            if(null != vo.getLsDt()){
                vo.setLsDtStr(DateUtils.formatDateToString(vo.getLsDt(),DateUtils.YYYY_MM_DD));
            }
            if(null != vo.getStlDt()){
                vo.setStlDtStr(DateUtils.formatDateToString(vo.getStlDt(),DateUtils.YYYY_MM_DD));
            }
            if(StringUtils.isNotEmpty(vo.getEachEstRntAmt())){
                vo.setEachEstRntAmtStr(df.format(DecimalUtils.convertFenToYuan(vo.getEachEstRntAmt())));
            }
            if(StringUtils.isNotEmpty(vo.getContAmt())){
                vo.setContAmtStr(df.format(DecimalUtils.convertFenToYuan(vo.getContAmt())));
            }
//            if(StringUtils.isNotEmpty(vo.getRntAmt())){
//                vo.setRntAmtStr(df.format(DecimalUtils.convertFenToYuan(vo.getRntAmt())));
//            }
        }
    }
	
	/**
     * 添加数据字典名称
     * @param voList
     */
	private void setDictData(PblBillMVO vo){
        if(null==vo) return;
        Map<String, String> crTypCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), PblBillMConstant.PBL_CR_TYP_CD);
        Map<String, String> loanStsCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), PblBillMConstant.PBL_LOAN_STS_CD);
        Map<String, String> loanBizTypCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), PblBillMConstant.PBL_LOAN_BIZ_TYP_CD);
        Map<String, String> lsPdUntCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), PblBillMConstant.PBL_LS_PD_UNT_CD);
        Map<String, String> payWayCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(),PblBillMConstant.PBL_PAY_WAY_CD);
        //格式化保留两位小数
        DecimalFormat df = new DecimalFormat("######0.00");
      
        if (crTypCdMap != null && crTypCdMap.containsKey(vo.getCrTypCd())) {
            vo.setCrTypCdNm(crTypCdMap.get(vo.getCrTypCd()));
        }
        if (loanStsCdMap != null && loanStsCdMap.containsKey(vo.getLoanStsCd())) {
            vo.setLoanStsCdNm(loanStsCdMap.get(vo.getLoanStsCd()));
        }
        if (loanBizTypCdMap != null && loanBizTypCdMap.containsKey(vo.getLoanBizTypCd())) {
            vo.setLoanBizTypCdNm(loanBizTypCdMap.get(vo.getLoanBizTypCd()));
        }
        if (lsPdUntCdMap != null && lsPdUntCdMap.containsKey(vo.getLsPdUntCd())) {
            vo.setLsPdUntCdNm(lsPdUntCdMap.get(vo.getLsPdUntCd()));
        }
        if (payWayCdMap != null && payWayCdMap.containsKey(vo.getPayWayCd())) {
            vo.setPayWayCdNm(payWayCdMap.get(vo.getPayWayCd()));
        }

        if(null != vo.getLsDt()){
            vo.setLsDtStr(DateUtils.formatDateToString(vo.getLsDt(),DateUtils.YYYY_MM_DD));
        }
        if(null != vo.getStlDt()){
            vo.setStlDtStr(DateUtils.formatDateToString(vo.getStlDt(),DateUtils.YYYY_MM_DD));
        }
        if(StringUtils.isNotEmpty(vo.getEachEstRntAmt())){
            vo.setEachEstRntAmtStr(df.format(DecimalUtils.convertFenToYuan(vo.getEachEstRntAmt())));
        }
        if(StringUtils.isNotEmpty(vo.getContAmt())){
        	vo.setContAmtStr(df.format(DecimalUtils.convertFenToYuan(vo.getContAmt())));
        }
//            if(StringUtils.isNotEmpty(vo.getRntAmt())){
//                vo.setRntAmtStr(df.format(DecimalUtils.convertFenToYuan(vo.getRntAmt())));
//            }
       
    }
}
